package com.loan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loan.entity.*;
import com.loan.entity.Process;
import com.loan.exception.CustomerException;
import com.loan.exception.ProductException;
import com.loan.mappers.*;
import com.loan.service.ApplicationService;
import com.loan.vo.ApplyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: ApplicationServiceImpl
 * @Author: wYin
 * @Date: 2022/1/18 10:47
 */
@Service("/applicationService")
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements ApplicationService {
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private MortgageMapper mortgageMapper;

    @Autowired
    private ProcessMapper processMapper;

    /**
     * 添加申请表信息
     *
     * @param application
     * @return
     */
    @Override
    @Transactional
    public boolean insert(Application application, Integer empId) {

        Customer customer = customerMapper.selectById(application.getCustomerId());
        //判断当前客户是否存在
        if (customer == null) {
            throw new CustomerException("当前客户的id不存在");
        }
        //判断当前产品是否存在
        Product product = productMapper.selectById(application.getProId());
        if (product == null) {
            throw new ProductException("当前产品不存在");
        }

        //设置申请时间
        application.setApplyTime(new Date());

        //执行添加申请
        int row = applicationMapper.insert(application);
        //修改申请表的抵押物的appid
        Mortgage mortgage = new Mortgage();
        mortgage.setAppId(application.getId());
        List<Integer> mortgageIds = application.getMortgageIds();
        for (Integer mortgageId : mortgageIds) {
            mortgage.setId(mortgageId);
            mortgageMapper.updateById(mortgage);
        }
        //添加操作记录
        Process process = new Process();

        process.setAppId(application.getId());
        process.setTime(new Date());
        process.setEmpId(empId);
        process.setHandleId(1);
        process.setResult("提交申請");

        processMapper.insert(process);

        if (row > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<Application> findByCondition(Application application) {
        return applicationMapper.findByCondition(application);
    }

    @Override
    @Transactional
    public void examine(ApplyVO applyVO) {
        //更新申请表状态
        Integer appId = applyVO.getAppId();
        Application application = applicationMapper.selectById(appId);
        application.setStatus(applyVO.getStatus());
        applicationMapper.updateById(application);

        //更新操作记录
        Process process = new Process();
        process.setHandleId("2".equals(applyVO.getStatus()) ? Handle.APPROVED : Handle.REJECTED);
        process.setAppId(appId);
        process.setResult(applyVO.getResult());
        process.setEmpId(applyVO.getEmpId());
        process.setTime(new Date());
        processMapper.insert(process);
    }
}
